from datetime import datetime

from django.template import RequestContext
from django.shortcuts import get_object_or_404, render_to_response

from merengue.base.views import content_view, content_list

from plugins.event.models import Event
from plugins.sportteam.models import Technique, Player, Chronicle, TournamentType, Tournament


def technique_view(request, technique_slug):
    technique = get_object_or_404(Technique, slug=technique_slug)
    return content_view(request, technique,
                        extra_context={})


def player_index(request):
    players = Player.objects.published()
    return render_to_response('sportteam/player_index.html', {'players': players},
                              context_instance=RequestContext(request))


def player_view(request, player_slug):
    player = get_object_or_404(Player, slug=player_slug)
    return content_view(request, player,
                        extra_context={})


def chronicle_view(request, chronicle_slug):
    sportpage = get_object_or_404(Chronicle, slug=chronicle_slug)
    return content_view(request, sportpage,
                        extra_context={})


def tournamenttype_view(request, tournamenttype_slug):
    tournamenttype = get_object_or_404(TournamentType, slug=tournamenttype_slug)
    return content_view(request, tournamenttype,
                        extra_context={})


def tournament_index(request):
    future_events = Event.objects.actives().filter(tournament__isnull=False)
    past_events = Event.objects.filter(status='published').filter(end__lt=datetime.now(), tournament__isnull=False).distinct()
    tournaments = Tournament.objects.published()
    return render_to_response('sportteam/tournament_index.html',
                              {'future_events': future_events,
                               'past_events': past_events,
                               'tournaments': tournaments},
                               context_instance=RequestContext(request))


def tournament_view(request, tournament_slug):
    tournament = get_object_or_404(Tournament, slug=tournament_slug)
    return content_view(request, tournament,
                        extra_context={})
